﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EnterpriseManagement.Models;

namespace EnterpriseManagement
{
    public partial class Employee
    {
        public Employee() { }

        /// <summary>
        /// Create Employee with RegisterModel Information
        /// </summary>
        /// <param name="model"></param>
        public Employee(BasicInformationModel model)
        {
            this.employee_id = model.EmployeeId;
            this.birthday = model.Birthday;
            this.sex = model.Sex;
            this.position = model.Position;
            this.major = model.Major;
            this.mobile = model.Mobile;
            this.hired_date = model.HiredDate;
            this.leave_date = model.LeaveDate;
            this.graduate_school = model.GraduateSchool;
            this.employee_status = model.EmployeeStatus;
            this.email = model.Email;
            this.name = model.Name;
        }

        public Employee(RegisterModel model)
        {
            this.employee_id = model.EmployeeId;
            this.birthday = model.Birthday;
            this.sex = model.Sex;
            this.position = model.Position;
            this.major = model.Major;
            this.mobile = model.Mobile;
            this.hired_date = model.HiredDate;
            this.leave_date = model.LeaveDate;
            this.graduate_school = model.GraduateSchool;
            this.employee_status = model.EmployeeStatus;
            this.email = model.Email;
            this.name = model.Name;
        }

        public void Update(Employee employee)
        {
            this.employee_id = employee.employee_id;
            this.birthday = employee.birthday;
            this.sex = employee.sex;
            this.position = employee.position;
            this.major = employee.major;
            this.mobile = employee.mobile;
            this.hired_date = employee.hired_date;
            this.leave_date = employee.leave_date;
            this.graduate_school = employee.graduate_school;
            this.employee_status = employee.employee_status;
            this.email = employee.email;
            this.name = employee.name;
        }
    }

    public partial class ProjectDetail
    {
        public ProjectDetail() { }

        /// <summary>
        /// 初始化ProjectDetail实例，除三个指定参数有值和Endtime=null,numberofpeople=1外，其他字段都置空
        /// clientname默认为数据库中第一个客户，具体数据由项目经理操作；
        /// </summary>
        /// <param name="projectName"></param>
        /// <param name="managerId"></param>
        /// <param name="starttime"></param>
        public ProjectDetail(string projectName, string managerId, string starttime)
        {
            ProjectEntities context = new ProjectEntities();
            var clientname = from client in context.Clients
                             select client.client_name;

            this.business_field = string.Empty;
            this.client_name = clientname.First();
            this.department = string.Empty;
            this.endtime = null;
            this.importance = string.Empty;
            this.manager_id = managerId;
            this.project_type = string.Empty;
            this.starttime = starttime;
            this.number_of_people = 0;
            this.project_name = projectName;
            this.main_technology = string.Empty;
            this.project_status = "On Progress";

            context.Dispose();
        }

        /// <summary>
        /// ProjectDetail转化为PorjectDetailsModel类型
        /// </summary>
        /// <returns></returns>
        public ProjectDetailsModel ChangetoProjectDetailsModel()
        {
            ProjectDetailsModel model = new ProjectDetailsModel();
            model.ProjectName=this.project_name;
            model.ProjectType=this.project_type;
            model.Importance=this.importance;
            model.MainTechnology=this.main_technology;
            model.StartTime=this.starttime;
            model.EndTime=this.endtime;
            model.BusinessField=this.business_field;
            model. ClientName=this.client_name;
            model.Department=this.department;
            model. Manager=DbHelper.GetEmployeeById (this.manager_id).name;
            model.NumberOfPeople =this.number_of_people;
            model.ProjectStatus=this.project_status;
            model.ManagerId = this.manager_id;

            return model;
        }

        public void Update(ProjectDetailsModel model)
        {
            this.project_name = model.ProjectName;
            this.project_type = model.ProjectType;
            this.importance = model.Importance;
            this.main_technology = model.MainTechnology;
            this.starttime = model.StartTime;
            this.endtime = model.EndTime;
            this.business_field = model.BusinessField;
            this.client_name = model.ClientName;
            this.department = model.Department;
            this.manager_id = (int.Parse (model.ManagerId)==0?this .manager_id :model.ManagerId);
            this.number_of_people = (model.NumberOfPeople==0?this .number_of_people :model .NumberOfPeople);
            this.project_status = model.ProjectStatus;
        }
    }

    public partial class TeamMember
    {
        public TeamMember() { }
        public TeamMember(string projectName,string employeeId,string position,string directorId=null,string starttime=null)
        {
            if (directorId == null)
                director_id = employeeId;
            else
                director_id = directorId;
                
            if (starttime == null)
                start_time = FormsService.GetFormatNowDate();
            else
                start_time = starttime;

            if (string.IsNullOrEmpty(position))
            {
                this.position = DbHelper.GetAllPositions().First();
            }
            else
            {
                this.position = position;
            }
            this.project_name = projectName;
            this.employee_id = employeeId;
        }

        public TeamMemberModel ChangeToTeamMemberModel()
        {
            TeamMemberModel tmModel = new TeamMemberModel();
            tmModel.ProjectName = this.project_name;
            tmModel.EmployeeId = this.employee_id;
            tmModel.EmployeeName = DbHelper.GetEmployeeById(this.employee_id).name;
            tmModel.Position = this.position;
            tmModel.Director = DbHelper.GetEmployeeById(this.director_id).name;
            tmModel.StartTime = this.start_time;
            tmModel.EndTime = this.end_time;
            tmModel.DirectorId = this.director_id;
            
            return tmModel;
        }

        public void UpdateTeammember(TeamMemberModel model)
        {
            this.project_name = model.ProjectName;
            this.employee_id = model.EmployeeId;
            this.position = model.Position;
            this.director_id = model.DirectorId;
            this.start_time = model.StartTime;
            this.end_time = model.EndTime;
        }
    }
}